package com.supermap.util.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;


import com.supermap.pisaclient.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Copyright (C) 2017 北京超图软件股份有限公司 版权所有
 * <p>
 * 功能描述：所有fragment的基类 作者：李会林
 * <p>
 * 修改人： 修改描述： 修改日期
 */
public abstract class BaseFragment extends Fragment {
    protected LinearLayout mContainer;
    private View mFragmentProgress;
    private View mFragmentFailed;
    private View mFragmentEmpty;
    // 加载框是否可以取消
    private boolean isCancelable = false;
    private List<String> mRequestList;
    private ProgressDialog mProgressDialog;
    protected Context mContext;

    public abstract View getContainerView();

    public abstract void initData();

    private boolean mPagePause = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRequestList = new ArrayList<String>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, null);
        mContainer = (LinearLayout) view.findViewById(R.id.base_fragment_container);
        mFragmentProgress = view.findViewById(R.id.base_fragment_progress);
        mFragmentProgress.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        mFragmentFailed = view.findViewById(R.id.base_fragment_failed);
        mFragmentEmpty = view.findViewById(R.id.base_fragment_empty);
        setBackground(view);
        View containerView = getContainerView();
        addContainerView(containerView);
        initData();
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    public final void addContainerView(View view) {
        if (view != null) {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            view.setLayoutParams(params);
            mContainer.removeAllViews();
            mContainer.addView(view);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mPagePause = false;
    }

    @Override
    public void onPause() {
        super.onPause();
        mPagePause = true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mRequestList.clear();
    }

    private void setBackground(View root) {
        if (!needSetFragmentBackground()) {
            return;
        }

        if (root != null) {
            root.setBackgroundColor(ContextCompat.getColor(mContext, R.color.common_grey_bg_color));
        }
    }

    protected boolean needSetFragmentBackground() {
        return true;
    }
}
